package com.muki.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JmsPreparedConnection {

	private Connection connection = null;
	private final String url = "www.qixuezhi.com:61616";
	private final String prefix = "tcp://";
	private final String brokerURL = prefix+url;

	public Session init() {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, 
				ActiveMQConnection.DEFAULT_PASSWORD, brokerURL);
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			return session;
		} catch (JMSException e) {
			e.printStackTrace();
		} 
		return null;
	}

	public void close() throws JMSException {
		if(connection!=null)
			connection.close();
	}
}
